using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.Domain.Model;
using SapaFinance.GenericRepository;

namespace SapaFinance.OOMap.Helpers
{
    public class CFOToSelectListConvert : TypeConverter<CFO, IEnumerable<SelectListItem>>
    {
        private readonly IGenericRepository<CFO, Guid> _repository;

        public CFOToSelectListConvert(IGenericRepository<CFO, Guid> repository)
        {
            _repository = repository;
        }

        protected override IEnumerable<SelectListItem> ConvertCore(CFO source)
        {
            var all = _repository.GetAll();
            if (source == null)
            {
                return all.Select(x => new SelectListItem
                                       {
                                           Value = x.UID.ToString(),
                                           Text = x.Name,
                                           Selected = false
                                       });
            }
            return all.Select(x => new SelectListItem
                                   {
                                       Value = x.UID.ToString(),
                                       Text = x.Name,
                                       Selected = source.UID == x.UID
                                   });
        }
    }
}